package com.emm.yixun.estate.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.jws.WebService;

import com.emm.yixun.common.model.enums.MerchantEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.emm.yixun.basic.enums.ModelEnum;
import com.emm.yixun.basic.service.BaseCodeTypeService;
import com.emm.yixun.basic.service.BaseCodeValueService;
import com.emm.yixun.basic.service.ImportlogService;
import com.emm.yixun.basic.service.ModelKeyService;
import com.emm.yixun.basic.service.ProjectBuildingService;
import com.emm.yixun.basic.service.ProjectCarareaService;
import com.emm.yixun.basic.service.ProjectCarportService;
import com.emm.yixun.basic.service.ProjectHouseTypeService;
import com.emm.yixun.basic.service.ProjectPremisesIndustDataService;
import com.emm.yixun.basic.service.ProjectPremisesService;
import com.emm.yixun.basic.service.ProjectRoomService;
import com.emm.yixun.basic.service.ProjectService;
import com.emm.yixun.basic.service.ProjectUnitService;
import com.emm.yixun.common.enums.IndustMarkEnums;
import com.emm.yixun.common.exception.BusinessException;
import com.emm.yixun.common.model.BaseCodeType;
import com.emm.yixun.common.model.BaseCodeValue;
import com.emm.yixun.common.model.Project;
import com.emm.yixun.common.model.ProjectBuilding;
import com.emm.yixun.common.model.ProjectCararea;
import com.emm.yixun.common.model.ProjectHouseType;
import com.emm.yixun.common.model.ProjectPremises;
import com.emm.yixun.common.model.ProjectPremisesIndustData;
import com.emm.yixun.common.model.ProjectRoom;
import com.emm.yixun.common.model.ProjectUnit;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.ApartmentModel;
import com.emm.yixun.common.model.dto.BaseCodeTypeDto;
import com.emm.yixun.common.model.dto.BaseCodeValueDto;
import com.emm.yixun.common.model.dto.CarModel;
import com.emm.yixun.common.model.dto.ImportlogDto;
import com.emm.yixun.common.model.dto.ProjectBuildingDto;
import com.emm.yixun.common.model.dto.ProjectCarareaDto;
import com.emm.yixun.common.model.dto.ProjectCarportDto;
import com.emm.yixun.common.model.dto.ProjectHouseTypeDto;
import com.emm.yixun.common.model.dto.ProjectPremisesDto;
import com.emm.yixun.common.model.dto.ProjectPremisesIndustDataDto;
import com.emm.yixun.common.model.dto.ProjectRoomDto;
import com.emm.yixun.common.model.dto.ProjectUnitDto;
import com.emm.yixun.common.model.dto.VillaModel;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.model.enums.PropertyTypeEnum;
import com.emm.yixun.common.model.enums.RoomStatusEnum;
import com.emm.yixun.common.utils.BeanUtil;
import com.emm.yixun.common.utils.SequencesGenerator;
import com.emm.yixun.estate.service.ImportExcelServiceFacade;

import net.sf.oval.ConstraintViolation;
import net.sf.oval.Validator;

@Service("importExcelServiceFacade")
@WebService(name = "com.emm.yixun.estate.service.ImportExcelServiceFacade")
public class ImportExcelServiceFacadeImpl implements ImportExcelServiceFacade {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Resource
	private ProjectService projectService;

	@Resource
	private ImportlogService importlogService;

	@Resource
	private ProjectPremisesService premisesService;

	@Resource
	private ProjectBuildingService projectBuildingService;

	@Resource
	private ProjectUnitService projectUnitService;

	@Resource
	private ProjectHouseTypeService projectHouseTypeService;

	@Resource
	private ProjectRoomService projectRoomService;

	@Resource
	private ModelKeyService modelKeyService;

	@Resource
	private ProjectCarareaService carareaService;

	@Resource
	private ProjectCarportService projectCarportService;
	
	@Resource
	private BaseCodeTypeService baseCodeTypeService;

	@Resource
	private BaseCodeValueService baseCodeValueService;

	@Resource
	private ProjectPremisesIndustDataService projectPremisesIndustDataService;

	@Resource
	private ProjectPremisesService projectPremisesService;

	@Override
	public Map<String, String> importApartment(List<ApartmentModel> list, Long id, User user) {
		String no = SequencesGenerator.getUUID("BTN");
		// 成功
		Long success = new Long(list.size() + 1);
		Long error = new Long(list.size() - 1);
		Long et = 0l;
		Map<String, String> map = new HashMap<String, String>();
		try {
			boolean specialFlag = false;
			if(MerchantEnum.GREEN_TOWN.getNo().equals(String.valueOf(user.getMerchantId()))
					|| MerchantEnum.GREEN_TOWN_TEST.getNo().equals(String.valueOf(user.getMerchantId()))){
				specialFlag = true;
			}
			// 组团ID
			Long premisesId = null;
			// 楼栋ID
			Long buildingId = null;
			// 单元ID
			Long unitId = null;
			// 户型ID
			Long houseTypeId = null;
			Map<String, String> result = vaildIndust(vaildIndustApartment(list));
			if (!StringUtils.isEmpty(result.get("error")) && specialFlag) {
				et = new Long(list.size());
				saveImportLog(result.get("error") + "业态不统一,请修改", "0", "请校验数据正确性!", error++, no, user);
			} else {
				for (int i = 0; i < list.size(); i++) {
					ApartmentModel apartmentModel = list.get(i);
					// 组团和楼栋不能同时为空
					// if (apartmentModel != null &&
					// org.apache.commons.lang.StringUtils.isBlank(apartmentModel.getExcel_0())
					// && org.apache.commons.lang.StringUtils.isBlank(apartmentModel.getExcel_1()))
					// {
					if (apartmentModel != null
							&& org.apache.commons.lang.StringUtils.isBlank(apartmentModel.getExcel_1())) {
						saveImportLog(getHouseName(apartmentModel, id), "0", "第" + (i + 1) + "条数据," + "楼栋不能为空", error--,
								no, user);
						et++;
						continue;
					}
					// 楼栋和单元不能同时为空
					// if (apartmentModel != null &&
					// org.apache.commons.lang.StringUtils.isBlank(apartmentModel.getExcel_0())
					// && org.apache.commons.lang.StringUtils.isBlank(apartmentModel.getExcel_2()))
					// {
					// saveImportLog(getHouseName(apartmentModel, id), "0", "第" + (i + 1) + "条数据," +
					// "楼栋和单元不能同时为空",
					// error--, no, user);
					// et++;
					// continue;
					// }
					Validator validator = new Validator();
					List<ConstraintViolation> ret = validator.validate(apartmentModel);
					if (ret != null && ret.size() > 0) {
						saveImportLog(getHouseName(apartmentModel, id), "0",
								"第" + (i + 1) + "条数据," + ret.get(0).getMessage(), error--, no, user);
						et++;
						continue;
					} else {

						// 校验数据准确性
						if (PropertyTypeEnum.getByDescription(apartmentModel.getExcel_5()) == null) {
							saveImportLog(getHouseName(apartmentModel, id), "0", "第" + (i + 1) + "条数据,房产类型不存在!",
									error--, no, user);
							et++;
							continue;
						}

						/*
						 * if (RoomStatusEnum.getByDescription(apartmentModel.getExcel_15()) == null) {
						 * saveImportLog(getHouseName(apartmentModel, id), "0", "第" + (i + 1) +
						 * "条数据,房间状态不存在!", error--, no, user); et++; continue; }
						 */

						// 校验业态是否正常
						// if (IndustMarkEnums.getByName(apartmentModel.getExcel_17()) == null) {
						// saveImportLog(getHouseName(apartmentModel, id), "0", "第" + (i + 1) +
						// "条数据,业态不存在!", error--, no,
						// user);
						// et++;
						// continue;
						// }

						// 验证户型数据是否正确

						// 查询组团名称是否存在
						premisesId = findProjectPremises(id, apartmentModel.getExcel_0(), user);
						// 如果楼层存在,先验证组团下面存在不存在这个楼栋
						if (!StringUtils.isEmpty(apartmentModel.getExcel_1())) {
							buildingId = findBuilding(premisesId, id, apartmentModel.getExcel_1(), user);
						}
						String unitName = "未设置";
						if (!StringUtils.isEmpty(apartmentModel.getExcel_2())) {
							unitName = apartmentModel.getExcel_2();
						}
						// 单元信息
						ProjectUnitDto unitDto = new ProjectUnitDto();
						unitDto.setProjectId(id);
						unitDto.setPremisesId(premisesId);
						unitDto.setBuildingId(buildingId);
						unitDto.setUnitName(unitName);
						unitDto.setDelFlag("0");
						List<ProjectUnit> unitList = projectUnitService.selectListByDto(unitDto);
						if (unitList != null && unitList.size() > 0) {
							unitId = unitList.get(0).getId();
						} else {
							unitId = saveProjectUnit(id, buildingId, premisesId, unitName, user);
						}
						// 户型信息
						houseTypeId = findHouseType(id, apartmentModel.getExcel_6(), apartmentModel.getExcel_8(), user,
								apartmentModel.getExcel_7());
						// 房间信息
						ProjectRoomDto roomDto = new ProjectRoomDto();
						roomDto.setPremisesId(premisesId);
						roomDto.setUnitId(unitId);
						roomDto.setBuildingId(buildingId);
						roomDto.setProjectId(id);
						roomDto.setRoomName(isNumeric(apartmentModel.getExcel_4()) == true
								? new Double(apartmentModel.getExcel_4()).intValue() + ""
								: apartmentModel.getExcel_4());
						roomDto.setFloor(new Integer(new Double(apartmentModel.getExcel_3()).intValue()));
						List<com.emm.yixun.common.model.ProjectRoom> roomList = projectRoomService
								.selectListByDto(roomDto);
						if (roomList != null && roomList.size() > 0) {
							// 信息重复，防止导入
							saveImportLog(getHouseName(apartmentModel, id), "0", "第" + (i + 1) + "条数据,房号重复", error--,
									no, user);
							et++;
							continue;
						} else {
							// 保存房间信息
							ApartmentModel model = list.get(i);
							ProjectRoom projectRoom = new ProjectRoom();
							projectRoom.setCombinedName(getHouseName(model, id));
							projectRoom.setRoomName(isNumeric(apartmentModel.getExcel_4()) == true
									? new Double(apartmentModel.getExcel_4()).intValue() + ""
									: apartmentModel.getExcel_4());
							projectRoom.setFloor(new Integer(new Double(model.getExcel_3()).intValue()));
							projectRoom.setBuildingArea(new BigDecimal(model.getExcel_8()));
							projectRoom.setInnerArea(
									new BigDecimal(StringUtils.isEmpty(model.getExcel_9()) ? "0" : model.getExcel_9()));
							projectRoom.setSharingArea(new BigDecimal(
									StringUtils.isEmpty(model.getExcel_10()) ? "0" : model.getExcel_10()));
							projectRoom.setFreeArea(new BigDecimal(
									StringUtils.isEmpty(model.getExcel_11()) ? "0" : model.getExcel_11()));
							projectRoom.setBasementArea(new BigDecimal(
									StringUtils.isEmpty(model.getExcel_12()) ? "0" : model.getExcel_12()));
							projectRoom.setAboveGroundArea(new BigDecimal("0"));
							projectRoom.setGardenArea(new BigDecimal("0"));
							// projectRoom.setRemark(model.getExcel_16());
							projectRoom.setPropertyType(
									PropertyTypeEnum.getByDescription(apartmentModel.getExcel_5()).getNo());
							// projectRoom.setIndustMark(IndustMarkEnums.getByName(apartmentModel.getExcel_17()).getCode());

							projectRoom.setRoomStatus(RoomStatusEnum.SALEL.getNo());
							// 去掉预售证编号及领取日期
							// projectRoom.setSalesPermissionNo(model.getExcel_13());
							// Date dt = vaildDate(model.getExcel_14());
							// if(dt!=null){
							// projectRoom.setSalesPermissionDate(dt);
							// }
							projectRoom.setIsRoom("1");
							if(specialFlag){
								String industIdMark = getIndustId(apartmentModel.getExcel_17(), apartmentModel.getExcel_18());
								if (StringUtils.isEmpty(industIdMark)) {
									// 信息重复，防止导入
									saveImportLog("业态:" + apartmentModel.getExcel_18() + "不存在", "0", "数据错误", error--, no, user);
									et++;
									continue;
								}
								projectRoom.setIndustMark(industIdMark);
							}

							saveProjectRoom(id, buildingId, premisesId, unitId, houseTypeId, projectRoom, user);
							saveImportLog(getHouseName(apartmentModel, id), "1", "第" + (i + 1) + "条数据,导入成功", success++,
									no, user);
						}
					}
				}
				industMarkSummary(result, id, 1L, user);
			}

		} catch (Exception e) {
			saveImportLog("系统异常", "0", "请校验数据正确性!", error++, no, user);
			et++;
			logger.error("ImportExcelServiceFacadeImpl importApartment error: ", e);
		} finally {
			map.put("result", no);
			map.put("total", String.valueOf(list.size()));
			map.put("et", String.valueOf(et));
			map.put("st", String.valueOf(list.size() - et));
		}
		return map;
	}

	/**
	 * @Title: industMarkSummary @Description: 导入时，触发汇总 @param @param map
	 *         key=组团名称-楼栋名称 ,value=楼栋唯一业态名称 @param isRoom @param projectId
	 *         设定文件 @return void 返回类型 @throws
	 */
	@Override
	public void industMarkSummary(Map<String, String> map, Long projectId, Long isRoom, User user) {
		if (user.getMerchantId() != 1L) {
			// 非pmo商户不执行该方法
			return;
		}

		StringBuffer info = new StringBuffer();
		for (Map.Entry<String, String> val : map.entrySet()) {
			if (StringUtils.isEmpty(val.getValue())) {
				continue;
			}
			String[] comboxValue = val.getKey().split("-");
			String groupName = comboxValue[0];
			String buildingName = null;
			if (comboxValue.length >= 2) {
				buildingName = comboxValue[1];
			}
			ProjectPremisesDto projectPremisesDto = new ProjectPremisesDto();
			projectPremisesDto.setProjectId(projectId);
			projectPremisesDto.setGroupName(groupName);
			projectPremisesDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			ProjectPremises projectPremises = projectPremisesService.selectByDto(projectPremisesDto);
			ProjectBuildingDto queryBuildingDto = new ProjectBuildingDto();
			queryBuildingDto.setBuildingName(buildingName);
			queryBuildingDto.setProjectId(projectId);
			queryBuildingDto.setPremisesId(projectPremises.getId());
			queryBuildingDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			List<ProjectBuilding> buildingList = projectBuildingService.selectListByDto(queryBuildingDto);
			ProjectBuilding building = new ProjectBuilding();
			if (buildingList != null && buildingList.size() > 1) {
				info.append("项目Id:" + projectId + ",组团ID:" + projectPremises.getId() + ",楼栋名称:" + buildingName
						+ "系统中存在多个,请修改</br>");
				continue;
			} else {
				if (buildingList.size() > 0) {
					building = buildingList.get(0);
				} else {
					continue;
				}
			}
			if (building != null) {
				// 修改当前楼栋下面所有房间的业态
				ProjectRoomDto roomEntity = new ProjectRoomDto();
				roomEntity.setProjectId(projectId);
				roomEntity.setBuildingId(building.getId());
				roomEntity.setDelFlag(DelFlagEnum.UNDEL.getNo());
				List<ProjectRoom> roomList = projectRoomService.selectListByDto(roomEntity);
				if (roomList != null) {
					for (ProjectRoom room : roomList) {
						ProjectRoomDto roomDto = new ProjectRoomDto();
						BeanUtil.convert2T(roomDto, room);
						String industId = getIndustId(val.getValue());
						if (industId != null) {
							roomDto.setIndustMark(industId);
						}
						projectRoomService.update(roomDto);
					}
				}
				ProjectPremisesIndustDataDto industData = new ProjectPremisesIndustDataDto();
				industData.setProjectId(projectId);
				industData.setBuildingId(building.getId());
				industData.setIndustType("2");
				BaseCodeValueDto baseCodeValue = new BaseCodeValueDto();
				industData.setPremisesId(projectPremises.getId());
				baseCodeValue.setCodeName(val.getValue());
				baseCodeValue.setDelFlag(DelFlagEnum.UNDEL.getNo());
				List<BaseCodeValue> baseCodeValueList = baseCodeValueService.selectListByDto(baseCodeValue);
				if (baseCodeValueList != null && baseCodeValueList.size() > 0) {
					industData.setIndustMark(baseCodeValueList.get(0).getId() + "");
				}

				ProjectRoomDto roomInfoEntity = projectRoomService.selectRoomInfoSummary(building.getId(), isRoom);
				if (roomInfoEntity != null) {
					Integer totalHouse = roomInfoEntity.getTotalHouse() == null ? null
							: new Integer(roomInfoEntity.getTotalHouse());
					BaseCodeValue codeEntity = null;
					String industId = getIndustId(val.getValue());
					if (!StringUtils.isEmpty(industId)) {
						codeEntity = baseCodeValueService.selectById(new Long(industId));
					}
					if (codeEntity != null) {
						if (codeEntity.getCodeName().equals(IndustMarkEnums.INDUSTMARKENUMS_PARKINSPACE.getName())) {
							industData.setSalableAcreag(new BigDecimal(totalHouse));
						}
					} else {
						BigDecimal div = new BigDecimal("10000");
						industData.setTotalHouse(totalHouse);
						industData.setNumberLayers(roomInfoEntity.getFloor());
						industData.setNumberOfBuildi(1);
						industData.setSalableAcreag(roomInfoEntity.getBuildingArea() == null ? null
								: roomInfoEntity.getBuildingArea().divide(div));
						industData.setGroundSalesAcreag(roomInfoEntity.getAboveGroundArea() == null ? null
								: roomInfoEntity.getAboveGroundArea().divide(div));
						industData.setUndergSalesAcreag(roomInfoEntity.getBasementArea() == null ? null
								: roomInfoEntity.getBasementArea().divide(div));
					}
					industData.setDelFlag(DelFlagEnum.UNDEL.getNo());
					industData.setCreateTime(new Date());
					industData.setUpdateTime(new Date());
				}
				ProjectPremisesIndustDataDto vaild = new ProjectPremisesIndustDataDto();
				vaild.setBuildingId(building.getId());
				vaild.setDelFlag(DelFlagEnum.UNDEL.getNo());
				vaild.setProjectId(projectId);
				List<ProjectPremisesIndustData> industVaild = projectPremisesIndustDataService.selectListByDto(vaild);
				if (industVaild != null && industVaild.size() > 0) {
					industData.setId(industVaild.get(0).getId());
					projectPremisesIndustDataService.update(industData);
				} else {
					projectPremisesIndustDataService.create(industData);
				}
			}
		}
		if (!StringUtils.isEmpty(info.toString())) {
			throw new BusinessException("100001", info.toString());
		}
	}

	public String getHouseName(ApartmentModel model, Long id) {
		Project project = projectService.selectById(id);
		String html = "";
		if (project != null) {
			html += project.getProjectName() + "-->" + model.getExcel_0();
		}
		if (!StringUtils.isEmpty(model.getExcel_1())) {
			html += "-->" + model.getExcel_1();
		}
		String lc = model.getExcel_3();
		String roomName = model.getExcel_4();
		html += "-->" + model.getExcel_2() + "-->" + lc + "-->" + roomName;
		return html;
	}

	/**
	 * 方法描述 : 保存导入日志 创建者：ThinkPad 项目名称： emm-website 类名： ProjectImportController.java
	 * 版本： v1.0 创建时间： 2016年5月26日 下午5:35:31
	 * 
	 * @param title
	 * @param result
	 * @param remark
	 * @param sort
	 *            void
	 */
	public void saveImportLog(String title, String result, String remark, Long sort, String no, User user) {
		ImportlogDto logdto = new ImportlogDto();
		logdto.setTitle(title);
		logdto.setResult(result);
		logdto.setRemark(remark);
		logdto.setSort(sort);
		logdto.setCreateTime(new Date());
		logdto.setDelFlag("0");
		logdto.setMerchantId(user.getMerchantId());
		logdto.setBatchCode(no);
		importlogService.create(logdto);
	}

	/**
	 * 方法描述 : 保存组团详情 创建者：ThinkPad 项目名称： emm-website 类名： ProjectImportController.java
	 * 版本： v1.0 创建时间： 2016年5月25日 下午6:46:54
	 * 
	 * @param premisesName
	 *            void
	 */
	public Long saveProjectPremises(String premisesName, Long projectId, User user) {
		ProjectPremisesDto premises = new ProjectPremisesDto();
		Project project = projectService.selectById(projectId);
		premises.setMerchantId(user.getMerchantId());
		premises.setUserCode(user.getUserCode());
		premises.setAccessCode(project.getProjectCode());
		premises.setProjectId(projectId);
		premises.setProjectCode(project.getProjectCode());
		premises.setGroupName(premisesName);
		premises.setDeliveryFlag("0");
		premises.setDelFlag("0");
		premises.setCreateUserId(user.getId());
		premises.setCreateTime(new Date());
		premises.setPremisesCode(modelKeyService.next(ModelEnum.PREMISES));
		return premisesService.create(premises).getId();
	}

	/**
	 * 方法描述 : 保存楼栋信息 创建者：ThinkPad 项目名称： emm-website 类名： ProjectImportController.java
	 * 版本： v1.0 创建时间： 2016年5月26日 上午10:29:32
	 * 
	 * @param premisesId
	 * @param building_name
	 * @return Long
	 */
	public Long saveProjectBuilding(Long premisesId, String building_name, Long projectId, User user) {
		ProjectBuildingDto buildingDto = new ProjectBuildingDto();
		Project project = projectService.selectById(projectId);
		buildingDto.setMerchantId(user.getMerchantId());
		buildingDto.setUserCode(user.getUserCode());
		buildingDto.setAccessCode(project.getProjectCode());
		buildingDto.setPremisesId(premisesId);
		buildingDto.setProjectId(projectId);
		buildingDto.setProjectCode(project.getProjectCode());
		buildingDto.setBuildingName(building_name);
		buildingDto.setDelFlag("0");
		buildingDto.setCreateUserId(user.getId());
		buildingDto.setCreateTime(new Date());
		// buildingDto.setBuildingCode(modelKeyService.next(ModelEnum.PROJECT_BUILDING));
		return projectBuildingService.create(buildingDto).getId();
	}

	/**
	 * 方法描述 : 保存楼栋信息 创建者：ThinkPad 项目名称： emm-website 类名： ProjectImportController.java
	 * 版本： v1.0 创建时间： 2016年5月26日 上午10:29:32
	 * 
	 * @param premisesId
	 * @param building_name
	 * @return Long
	 */
	public Long saveProjectBuilding(Long premisesId, String building_name, Long projectId, User user,
			String isCarArea) {
		ProjectBuildingDto buildingDto = new ProjectBuildingDto();
		Project project = projectService.selectById(projectId);
		buildingDto.setMerchantId(user.getMerchantId());
		buildingDto.setUserCode(user.getUserCode());
		buildingDto.setAccessCode(project.getProjectCode());
		buildingDto.setPremisesId(premisesId);
		buildingDto.setProjectId(projectId);
		buildingDto.setProjectCode(project.getProjectCode());
		buildingDto.setBuildingName(building_name);
		buildingDto.setDelFlag("0");
		buildingDto.setCreateUserId(user.getId());
		buildingDto.setCreateTime(new Date());
		buildingDto.setIsCarArea(isCarArea);
		// buildingDto.setBuildingCode(modelKeyService.next(ModelEnum.PROJECT_BUILDING));
		return projectBuildingService.create(buildingDto).getId();
	}

	/**
	 * 方法描述 : 保存单元信息 创建者：ThinkPad 项目名称： emm-estate 类名：
	 * ImportExcelServiceFacadeImpl.java 版本： v1.0 创建时间： 2016年5月27日 下午2:24:34
	 * 
	 * @param projectId
	 * @param buildingId
	 * @param premisesId
	 * @param unitName
	 * @param user
	 * @return Long
	 */
	public Long saveProjectUnit(Long projectId, Long buildingId, Long premisesId, String unitName, User user) {
		ProjectUnitDto dto = new ProjectUnitDto();
		dto.setMerchantId(user.getMerchantId());
		dto.setUserCode(user.getUserCode());
		Project project = projectService.selectById(projectId);
		dto.setAccessCode(project.getProjectCode());
		dto.setBuildingId(buildingId);
		dto.setPremisesId(premisesId);
		dto.setProjectId(projectId);
		dto.setProjectCode(project.getProjectCode());
		dto.setUnitName(unitName);
		dto.setDelFlag("0");
		dto.setCreateUserId(user.getId());
		dto.setCreateTime(new Date());
		dto.setUnitCode(modelKeyService.next(ModelEnum.PROJECT_UNIT));
		return projectUnitService.create(dto).getId();
	}

	/**
	 * 方法描述 : 保存户型信息 创建者：ThinkPad 项目名称： emm-website 类名： ProjectImportController.java
	 * 版本： v1.0 创建时间： 2016年5月26日 下午2:46:27
	 * 
	 * @param projectId
	 * @return Long
	 */
	public Long saveHouseType(Long projectId, String excel_6, String excel_8, User user, String fx)
			throws BusinessException {
		ProjectHouseTypeDto dto = new ProjectHouseTypeDto();
		dto.setMerchantId(user.getMerchantId());
		dto.setUserCode(user.getUserCode());
		Project project = projectService.selectById(projectId);
		dto.setAccessCode(project.getProjectCode());
		dto.setProjectId(projectId);
		dto.setProjectCode(project.getProjectCode());
		dto.setHouseTypeName(excel_6);
		dto.setInsideBuildingArea(new BigDecimal(excel_8));
		dto.setDelFlag("0");
		dto.setCreateUserId(user.getId());
		dto.setCreateTime(new Date());
		dto.setHouseTypeCode(modelKeyService.next(ModelEnum.PROJECTHOUSETYPE));
		if (!StringUtils.isEmpty(fx)) {
			String list = getNumbers(fx);
			String[] array = list.split(",");
			String roomNum = "0";
			String officeNum = "0";
			String guardNum = "0";
			for (int ii = 0; ii < array.length; ii++) {
				if (ii == 0 && org.apache.commons.lang.StringUtils.isNotEmpty(array[0])) {
					roomNum = array[0];
				}
				if (ii == 1 && org.apache.commons.lang.StringUtils.isNotEmpty(array[1])) {
					officeNum = array[1];
				}
				if (ii == 2 && org.apache.commons.lang.StringUtils.isNotEmpty(array[2])) {
					guardNum = array[2];
				}
			}
			dto.setRoomNum(Integer.valueOf(stringToNumber(roomNum)));
			dto.setOfficeNum(Integer.valueOf(stringToNumber(officeNum)));
			dto.setGuardNum(Integer.valueOf(stringToNumber(guardNum)));
		}

		return projectHouseTypeService.create(dto).getId();
	}

	/**
	 * 方法描述 : 保存房间信息 创建者：ThinkPad 项目名称： emm-website 类名： ProjectImportController.java
	 * 版本： v1.0 创建时间： 2016年5月26日 下午2:22:06
	 * 
	 * @param projectId
	 * @param buildingId
	 * @param premisesId
	 * @param unit
	 * @param model
	 * @return Long
	 */
	public void saveProjectRoom(Long projectId, Long buildingId, Long premisesId, Long unit, Long houseTypeId,
			ProjectRoom projectRoom, User user) {
		ProjectRoomDto roomDto = BeanUtil.convert2T(new ProjectRoomDto(), projectRoom);
		roomDto.setMerchantId(user.getMerchantId());
		roomDto.setUserCode(user.getUserCode());
		Project project = projectService.selectById(projectId);
		roomDto.setAccessCode(project.getProjectCode());
		roomDto.setBuildingId(buildingId);
		roomDto.setPremisesId(premisesId);
		roomDto.setUnitId(unit);
		roomDto.setProjectId(projectId);
		roomDto.setProjectCode(project.getProjectCode());

		roomDto.setHouseTypeId(houseTypeId);
		roomDto.setDelFlag("0");
		// roomDto.setSalesPermissionDate(java.sql.Date.valueOf(model.getExcel_14()));
		roomDto.setCreateUserId(user.getId());
		roomDto.setCreateTime(new Date());
		roomDto.setRoomCode(modelKeyService.next(ModelEnum.PROJECT_ROOM));
		projectRoomService.create(roomDto);
	}

	@Override
	public Map<String, String> importVilla(List<VillaModel> list, Long id, User user) {
		String no = SequencesGenerator.getUUID("BTN");
		Map<String, String> map = new HashMap<String, String>();
		Long success = new Long(list.size() + 1);
		Long error = new Long(list.size() - 1);
		Long et = 0L;
		// 组团id
		Long premisesId = null;
		// 楼栋ID
		Long buildingId = null;
		// 户型ID
		Long houseTypeId = null;
		try {
			boolean specialFlag = false;
			if(MerchantEnum.GREEN_TOWN.getNo().equals(String.valueOf(user.getMerchantId()))
					|| MerchantEnum.GREEN_TOWN_TEST.getNo().equals(String.valueOf(user.getMerchantId()))){
				specialFlag = true;
			}

			Map<String, String> result = vaildIndust(vaildIndustVilla(list));
			if (!StringUtils.isEmpty(result.get("error")) && specialFlag) {
				et = new Long(list.size());
				saveImportLog(result.get("error") + "业态不统一,请修改", "0", "请校验数据正确性!", error++, no, user);
			} else {
				for (int i = 0; i < list.size(); i++) {
					VillaModel villaModel = list.get(i);
					Validator validator = new Validator();
					List<ConstraintViolation> ret = validator.validate(villaModel);
					if (ret != null && ret.size() > 0) {
						saveImportLog(getMsg(villaModel, id), "0", "第" + (i + 1) + "条数据," + ret.get(0).getMessage(),
								error--, no, user);
						et++;
						continue;
					} else {
						// if (PropertyTypeEnum.getByDescription(villaModel.getExcel_3()) == null) {
						// saveImportLog(getMsg(villaModel, id), "0", "第" + (i + 1) + "条数据,房产类型不存在!",
						// error--, no, user);
						// et++;
						// continue;
						// }
						/*
						 * if (RoomStatusEnum.getByDescription(villaModel.getExcel_14()) == null) {
						 * saveImportLog(getMsg(villaModel, id), "0", "第" + (i + 1) + "条数据,房间状态不存在!",
						 * error--, no, user); et++; continue; }
						 */

						// 校验业态是否正常
						// if (IndustMarkEnums.getByName(villaModel.getExcel_16()) == null) {
						// saveImportLog(getMsg(villaModel, id), "0", "第" + (i + 1) + "条数据,业态不存在!",
						// error--, no, user);
						// et++;
						// continue;
						// }
						// 获取导入数据
						VillaModel model = list.get(i);

						// 组团信息
						premisesId = findProjectPremises(id, list.get(i).getExcel_0(), user);
						// 楼栋（排屋）
						buildingId = findBuilding(premisesId, id, list.get(i).getExcel_1(), user);
						// 户型信息
						houseTypeId = findHouseType(id, list.get(i).getExcel_4(), list.get(i).getExcel_6(), user, null);

						// 房间信息
						ProjectRoomDto roomDto = new ProjectRoomDto();
						roomDto.setPremisesId(premisesId);
						roomDto.setBuildingId(buildingId);
						roomDto.setProjectId(id);
						roomDto.setRoomName(list.get(i).getExcel_2());
						List<com.emm.yixun.common.model.ProjectRoom> roomList = projectRoomService
								.selectListByDto(roomDto);
						if (roomList != null && roomList.size() > 0) {
							// 信息重复，防止导入
							saveImportLog(getMsg(list.get(i), id), "0", "第" + (i + 1) + "条数据,别墅名字重复", error--, no,
									user);
							et++;
							continue;
						} else {
							// 保存别墅信息
							ProjectRoom projectRoom = new ProjectRoom();
							projectRoom.setCombinedName(getMsg(model, id));
							projectRoom.setRoomName(model.getExcel_2());
							projectRoom.setBuildingArea(new BigDecimal(model.getExcel_6()));// 建筑面积
							projectRoom.setInnerArea(
									new BigDecimal(StringUtils.isEmpty(model.getExcel_7()) ? "0" : model.getExcel_7())); // 套内建筑面积
							projectRoom.setSharingArea(
									new BigDecimal(StringUtils.isEmpty(model.getExcel_8()) ? "0" : model.getExcel_8())); // 分摊面积
							projectRoom.setGardenArea(
									new BigDecimal(StringUtils.isEmpty(model.getExcel_9()) ? "0" : model.getExcel_9())); // 花园面积
							projectRoom.setAboveGroundArea(new BigDecimal(
									StringUtils.isEmpty(model.getExcel_10()) ? "0" : model.getExcel_10()));// 地下室面积
							projectRoom.setBasementArea(new BigDecimal(
									StringUtils.isEmpty(model.getExcel_11()) ? "0" : model.getExcel_11()));// 地下室面积
							projectRoom.setFreeArea(new BigDecimal(
									StringUtils.isEmpty(model.getExcel_12()) ? "0" : model.getExcel_12()));// 赠送面积
							// projectRoom.setRemark(model.getExcel_15());
							projectRoom.setPropertyType(PropertyTypeEnum.getByDescription("别墅").getNo());
							// projectRoom.setIndustMark(IndustMarkEnums.getByName(model.getExcel_16()).getCode());
							projectRoom.setRoomStatus(RoomStatusEnum.SALEL.getNo());

							if(specialFlag){
								String industIdMark = getIndustId(model.getExcel_16(), model.getExcel_17());
								// 判断业态是否存在
								if (industIdMark == null) {
									saveImportLog("业态:" + model.getExcel_17() + "不存在", "0", "数据错误", error--, no, user);
									et++;
									continue;
								}
								projectRoom.setIndustMark(industIdMark);
							}

							// 去掉预售证编号及领取日期
							// projectRoom.setSalesPermissionNo(model.getExcel_12());
							// Date dt = vaildDate(model.getExcel_13());
							// if(dt!=null){
							// projectRoom.setSalesPermissionDate(dt);
							// }
							saveProjectRoom(id, buildingId, premisesId, null, houseTypeId, projectRoom, user);
							saveImportLog(getMsg(list.get(i), id), "1", "第" + (i + 1) + "条数据,导入成功", success++, no,
									user);
						}
					}
				}
				industMarkSummary(result, id, null, user);
			}
		} catch (Exception e) {
			saveImportLog("系统异常", "0", "请校验数据正确性!", error++, no, user);
			et++;
			logger.error("ImportExcelServiceFacadeImpl importVilla error: ", e);
		} finally {
			// if(et < 0 ){
			// et = 0L;
			// }else if(et > list.size()){
			// et = Long.parseLong(list.size()+"");
			// }
			map.put("result", no);
			map.put("total", String.valueOf(list.size()));
			map.put("et", String.valueOf(et));
			map.put("st", String.valueOf(list.size() - et));
		}
		return map;
	}

	public String getMsg(VillaModel valla, Long id) {
		Project project = projectService.selectById(id);
		String html = "";
		if (project != null) {
			html += project.getProjectName() + "-->" + valla.getExcel_0() + "-->" + valla.getExcel_1() + "-->"
					+ valla.getExcel_2();
		}
		return html;

	}

	/**
	 * 方法描述 : 组团存在不存在， 存在就返回ID， 不存在保存返回ID 创建者：ThinkPad 项目名称： emm-estate 类名：
	 * ImportExcelServiceFacadeImpl.java 版本： v1.0 创建时间： 2016年5月27日 下午3:21:39
	 * 
	 * @param projectId
	 * @param groupName
	 * @param user
	 * @return Long
	 */
	public Long findProjectPremises(Long projectId, String groupName, User user) {
		if (StringUtils.isEmpty(groupName)) {
			groupName = "未设置";
		}
		Long premisesId = null;
		ProjectPremisesDto dto = new ProjectPremisesDto();
		dto.setProjectId(projectId);
		dto.setGroupName(groupName);
		dto.setDelFlag("0");
		List<ProjectPremises> premises = premisesService.selectListByDto(dto);
		if (premises != null && premises.size() > 0) {
			// 组团名称已经存在
			premisesId = premises.get(0).getId();
		} else {
			// 保存信息
			premisesId = saveProjectPremises(groupName, projectId, user);
		}
		return premisesId;
	}

	/**
	 * 方法描述 : 返回楼栋ID 创建者：ThinkPad 项目名称： emm-estate 类名：
	 * ImportExcelServiceFacadeImpl.java 版本： v1.0 创建时间： 2016年5月27日 下午3:26:29
	 * 
	 * @param premisesId
	 * @param projectId
	 * @param buildingName
	 * @param user
	 * @return Long
	 */
	public Long findBuilding(Long premisesId, Long projectId, String buildingName, User user) {
		Long buildingId = null;
		if (StringUtils.isEmpty(buildingName)) {
			buildingName = "未设置";
		}
		ProjectBuildingDto buildingDto = new ProjectBuildingDto();
		buildingDto.setPremisesId(premisesId);
		buildingDto.setBuildingName(buildingName);
		buildingDto.setProjectId(projectId);
		List<ProjectBuilding> projectBuilding = projectBuildingService.selectListByDto(buildingDto);
		if (projectBuilding != null && projectBuilding.size() > 0) {
			buildingId = projectBuilding.get(0).getId();
		} else {
			buildingId = saveProjectBuilding(premisesId, buildingName, projectId, user);
		}
		return buildingId;
	}

	/**
	 * 方法描述 : 返回楼栋ID 创建者：ThinkPad 项目名称： emm-estate 类名：
	 * ImportExcelServiceFacadeImpl.java 版本： v1.0 创建时间： 2016年5月27日 下午3:26:29
	 * 
	 * @param premisesId
	 * @param projectId
	 * @param buildingName
	 * @param user
	 * @return Long
	 */
	public Long findBuilding(Long premisesId, Long projectId, String buildingName, User user, String isCarArea) {
		Long buildingId = null;
		if (StringUtils.isEmpty(buildingName)) {
			buildingName = "未设置";
		}
		ProjectBuildingDto buildingDto = new ProjectBuildingDto();
		buildingDto.setPremisesId(premisesId);
		buildingDto.setBuildingName(buildingName);
		buildingDto.setProjectId(projectId);
		List<ProjectBuilding> projectBuilding = projectBuildingService.selectListByDto(buildingDto);
		if (projectBuilding != null && projectBuilding.size() > 0) {
			buildingId = projectBuilding.get(0).getId();
		} else {
			buildingId = saveProjectBuilding(premisesId, buildingName, projectId, user, isCarArea);
		}
		return buildingId;
	}

	/**
	 * 方法描述 : 返回户型ID 创建者：ThinkPad 项目名称： emm-estate 类名：
	 * ImportExcelServiceFacadeImpl.java 版本： v1.0 创建时间： 2016年5月27日 下午3:29:59
	 * 
	 * @return Long
	 */
	public Long findHouseType(Long projectId, String houseTypeName, String mj, User user, String fx) {
		Long houseTypeId = null;
		if (StringUtils.isEmpty(houseTypeName))
			return houseTypeId;
		ProjectHouseTypeDto typeDto = new ProjectHouseTypeDto();
		typeDto.setProjectId(projectId);
		typeDto.setHouseTypeName(houseTypeName);
		typeDto.setDelFlag("0");
		List<ProjectHouseType> houseType = projectHouseTypeService.selectListByDto(typeDto);
		if (houseType != null && houseType.size() > 0) {
			houseTypeId = houseType.get(0).getId();
		} else {
			houseTypeId = saveHouseType(projectId, houseTypeName, mj.toString(), user, fx);
		}
		return houseTypeId;
	}

	@Override
	public Map<String, String> importCarInfo(List<CarModel> list, Long id, User user) {
		Map<String, String> map = new HashMap<String, String>();
		String no = SequencesGenerator.getUUID("BTN");
		// 成功
		Long success = new Long(list.size() + 1);
		Long error = new Long(list.size() - 1);
		Long premisesId = null;
		Long buildingId = null;
		Long et = 0l;
		try {
			boolean specialFlag = false;
			if(MerchantEnum.GREEN_TOWN.getNo().equals(String.valueOf(user.getMerchantId()))
					|| MerchantEnum.GREEN_TOWN_TEST.getNo().equals(String.valueOf(user.getMerchantId()))){
				specialFlag = true;
			}

			Map<String, String> result = vaildIndust(vaildIndustCarModel(list));
			if (!StringUtils.isEmpty(result.get("error"))) {
				et = new Long(list.size());
				saveImportLog(result.get("error") + "业态不统一,请修改", "0", "请校验数据正确性!", error++, no, user);
			} else {
				for (int i = 0; i < list.size(); i++) {
					CarModel car = list.get(i);
					Validator validator = new Validator();
					List<ConstraintViolation> ret = validator.validate(car);
					if (ret != null && ret.size() > 0) {
						saveImportLog(getCarMsg(car, id), "0", "第" + (i + 1) + "条数据," + ret.get(0).getMessage(),
								error--, no, user);
						et++;
						continue;
					} else {
						// 组团信息
						if (!StringUtils.isEmpty(car.getExcel_0())) {
							premisesId = findProjectPremises(id, car.getExcel_0(), user);
						}
						// 楼栋
						buildingId = findBuilding(premisesId, id, list.get(i).getExcel_1(), user, "1");
						// 车位
						ProjectRoom projectRoom = new ProjectRoom();
						projectRoom.setCombinedName(getCarMsg(car, id));
						projectRoom.setRoomName(car.getExcel_2());
						projectRoom.setBuildingArea(
								new BigDecimal(StringUtils.isEmpty(car.getExcel_3()) ? "0" : car.getExcel_3()));
						projectRoom.setInnerArea(
								new BigDecimal(StringUtils.isEmpty(car.getExcel_3_()) ? "0" : car.getExcel_3_()));
						projectRoom.setSharingArea(new BigDecimal("0"));
						projectRoom.setFreeArea(new BigDecimal("0"));
						projectRoom.setGardenArea(new BigDecimal("0"));
						projectRoom.setBasementArea(new BigDecimal("0"));
						projectRoom.setAboveGroundArea(new BigDecimal("0"));
						projectRoom.setPropertyType(PropertyTypeEnum.PARKING.getNo());
						projectRoom.setRoomStatus(RoomStatusEnum.SALEL.getNo());
						projectRoom.setIsRoom("2");
						if(specialFlag){
							String industId = getIndustId(car.getExcel_6(), car.getExcel_7());
							if (industId == null) {
								// 信息重复，防止导入
								saveImportLog("业态:" + car.getExcel_7() + "不存在", "0", "数据错误", error--, no, user);
								et++;
								continue;
							}
							projectRoom.setIndustMark(industId);
						}

						saveProjectRoom(id, buildingId, premisesId, null, null, projectRoom, user);
						saveImportLog(getCarMsg(car, id), "1", "第" + (i + 1) + "条数据,导入成功", success++, no, user);
					}
				}
				industMarkSummary(result, id, 2L, user);
			}

		} catch (Exception e) {
			logger.error("ImportExcelServiceFacadeImpl importCarInfo error: ", e);
			et++;
			saveImportLog("系统异常", "0", "请校验数据正确性!", error++, no, user);
		} finally {
			map.put("result", no);
			map.put("total", String.valueOf(list.size()));
			map.put("et", String.valueOf(et));
			map.put("st", String.valueOf(list.size() - et));
		}
		return map;
	}

	public String getCarMsg(CarModel car, Long id) {
		Project project = projectService.selectById(id);
		String html = "";
		if (project != null) {
			html += project.getProjectName() + "-->";
			if (!StringUtils.isEmpty(car.getExcel_0())) {
				html += car.getExcel_0();
			}
			html += "-->" + car.getExcel_1() + "-->" + car.getExcel_2();
		}
		return html;

	}

	/**
	 * 方法描述 : 车区， 返回ID 创建者：ThinkPad 项目名称： emm-estate 类名：
	 * ImportExcelServiceFacadeImpl.java 版本： v1.0 创建时间： 2016年5月27日 下午5:09:40
	 * 
	 * @param id
	 * @param carareaName
	 * @param user
	 * @return Long
	 */
	public Long findCarArea(Long id, String carareaName, User user, Long premisesId) {
		Long carAreaId = null;
		ProjectCarareaDto entity = new ProjectCarareaDto();
		entity.setCarareaName(carareaName);
		entity.setDelFlag("0");
		entity.setProjectId(id);
		entity.setPremisesId(premisesId);
		List<ProjectCararea> list = carareaService.selectListByDto(entity);
		if (list != null && list.size() > 0) {
			carAreaId = list.get(0).getId();

		} else {
			carAreaId = saveCarArea(id, carareaName, user, premisesId);
		}
		return carAreaId;
	}

	/**
	 * 方法描述 : 保存车区 创建者：ThinkPad 项目名称： emm-estate 类名：
	 * ImportExcelServiceFacadeImpl.java 版本： v1.0 创建时间： 2016年5月27日 下午5:23:37
	 * 
	 * @param id
	 * @param carareaName
	 * @param user
	 * @param premisesId
	 * @return Long
	 */
	public Long saveCarArea(Long id, String carareaName, User user, Long premisesId) {
		ProjectCarareaDto entity = new ProjectCarareaDto();
		entity.setCarareaCode(modelKeyService.next(ModelEnum.PROJECT_CAR_AREA));
		entity.setMerchantId(user.getMerchantId());
		entity.setUserCode(user.getUserCode());
		Project project = projectService.selectById(id);
		entity.setAccessCode(project.getProjectCode());
		entity.setCarareaName(carareaName);
		entity.setPremisesId(premisesId);
		entity.setProjectId(id);
		entity.setProjectCode(project.getProjectCode());
		entity.setDelFlag("0");
		entity.setCreateUserId(user.getId());
		entity.setCreateTime(new Date());
		return carareaService.create(entity).getId();
	}

	/**
	 * 方法描述 : 保存车位信息 创建者：ThinkPad 项目名称： emm-estate 类名：
	 * ImportExcelServiceFacadeImpl.java 版本： v1.0 创建时间： 2016年5月27日 下午5:31:55
	 * 
	 * @param id
	 * @param carPortName
	 * @param user
	 * @param premisesId
	 * @param carAreaId
	 * @return Long
	 */
	public Long saveCarport(Long id, String carPortName, User user, Long premisesId, Long carAreaId, CarModel car) {
		ProjectCarportDto entity = new ProjectCarportDto();
		entity.setCarportCode(modelKeyService.next(ModelEnum.PROJECT_CAR_PORT));
		entity.setMerchantId(user.getMerchantId());
		entity.setUserCode(user.getUserCode());
		Project project = projectService.selectById(id);
		entity.setAccessCode(project.getProjectCode());
		entity.setCarareaId(carAreaId);
		entity.setPremisesId(premisesId);
		entity.setProjectId(id);
		entity.setCombinedName(getCarMsg(car, carAreaId));
		entity.setCarportName(carPortName);
		// entity.setPropertyType(PropertyTypeEnum.getByDescription(car.getExcel_3()).getNo());
		entity.setPropertyType(PropertyTypeEnum.getByDescription("车位").getNo());
		// entity.setCarportSalesStatus(CarportSalesStatusEnum.getByDescription(car.getExcel_4()).getNo());
		entity.setDelFlag("0");
		entity.setCreateUserId(user.getId());
		entity.setCreateTime(new Date());
		// entity.setRemark(car.getExcel_5());
		return projectCarportService.create(entity).getId();

	}

	public static Date vaildDate(String date) {
		Date dt = null;
		try {
			SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
			dt = dfs.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
			return dt;
		}
		return dt;
	}

	public String getNumbers(String content) {
		StringBuffer bf = new StringBuffer();
		Pattern pattern = Pattern.compile("\\d+");
		Matcher matcher = pattern.matcher(content);
		while (matcher.find()) {
			bf.append(matcher.group() + ",");
		}
		return bf.toString();
	}

	public boolean isNumeric(String str) {
		try {
			Double.valueOf(str);
		} catch (Exception e) {
			// e.printStackTrace();
			return false;
		}
		return true;
	}

	public String stringToNumber(String number) {
		String result = "";
		if ("0123456789".indexOf(number) >= 0) {
			return number;
		}
		switch (number) {
		case "零":
			result = "0";
			break;
		case "一":
			result = "1";
			break;
		case "二":
			result = "2";
			break;
		case "三":
			result = "3";
			break;
		case "四":
			result = "4";
			break;
		case "五":
			result = "5";
			break;
		case "六":
			result = "6";
			break;
		case "七":
			result = "7";
			break;
		case "八":
			result = "8";
			break;
		case "九":
			result = "9";
			break;
		default:
			result = "0";
			break;
		}
		return result;
	}

	/**
	 * @Title: vaildIndust @Description: 当楼栋名称相同时，他们的业态必须是同一个 @param @param
	 *         list @param @return 设定文件 @return String 返回类型 @throws
	 */
	public Map<String, String> vaildIndust(Map<String, List<String>> map) {
		Map<String, String> result = new HashMap<String, String>();

		for (Map.Entry<String, List<String>> entry : map.entrySet()) {
			if (!hasSame(entry.getValue())) {
				result.put("error", entry.getKey());
				return result;
			} else {
				result.put(entry.getKey(), entry.getValue().get(0));
			}
		}
		return result;
	}

	/**
	 *
	 * @Title: vaildIndustApartment 房间 @Description: Map<String,List<String>>
	 *         key=楼栋,value=楼栋对于的所有的业态 @param @param list @param @return
	 *         设定文件 @return Map<String,List<String>> 返回类型 @throws
	 */
	public Map<String, List<String>> vaildIndustApartment(List<ApartmentModel> list) {
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		if (list != null && list.size() > 0) {
			for (ApartmentModel model : list) {
				String key = model.getExcel_0() + "-" + model.getExcel_1();
				if (map.get(key) == null) {
					List<String> array = new ArrayList<String>();
					array.add(model.getExcel_17());
					map.put(key, array);
				} else {
					List<String> array = map.get(key);
					array.add(model.getExcel_17());
					map.put(key, array);
				}
			}
		}
		return map;
	}

	/**
	 *
	 * @Title: vaildIndustVilla @Description: 别墅类型的业态模板 @param @param
	 *         list @param @return 设定文件 @return Map<String,List<String>>
	 *         返回类型 @throws
	 */
	public Map<String, List<String>> vaildIndustVilla(List<VillaModel> list) {
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		if (list != null && list.size() > 0) {
			for (VillaModel model : list) {
				String key = model.getExcel_0() + "-" + model.getExcel_1();
				if (map.get(key) == null) {
					List<String> array = new ArrayList<String>();
					array.add(model.getExcel_16());
					map.put(key, array);
				} else {
					List<String> array = map.get(key);
					array.add(model.getExcel_16());
					map.put(key, array);
				}
			}
		}
		return map;
	}

	public Map<String, List<String>> vaildIndustCarModel(List<CarModel> list) {
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		if (list != null && list.size() > 0) {
			for (CarModel model : list) {
				// Key = 组团,车区
				String key = model.getExcel_0() + "-" + model.getExcel_1();
				if (map.get(key) == null) {
					List<String> array = new ArrayList<String>();
					array.add(model.getExcel_6());
					map.put(key, array);
				} else {
					List<String> array = map.get(key);
					array.add(model.getExcel_6());
					map.put(key, array);
				}
			}
		}
		return map;
	}

	public static boolean hasSame(List<? extends Object> list) {
		if (null == list)
			return false;
		return 1 == new HashSet<Object>(list).size();
	}

	public String getIndustId(String codeName) {
		Long id = baseCodeValueService.queryBaseCodeValue(codeName);
		if (id == null) {
			return null;
		}
		return id.toString();
	}

	/**
	 * 获取小业态id
	 * @param typeName 大业态
	 * @param codeName 小业态
	 * @return
	 */
	public String getIndustId(String typeName, String codeName) {
		//先查询大业态
		BaseCodeTypeDto entity = new BaseCodeTypeDto();
		entity.setTypeName(typeName);
		entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
		BaseCodeType bct = baseCodeTypeService.selectByDto(entity);
		if(bct == null) {
			return null;
		}
		//再根据大业态id查询小业态
		BaseCodeValueDto dto = new BaseCodeValueDto();
		dto.setTypeId(bct.getId());
		dto.setCodeName(codeName);
		dto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		BaseCodeValue bcv = baseCodeValueService.selectByDto(dto);
		if (bcv == null) {
			return null;
		}
		return bcv.getId().toString();
	}
}
